package com.itqianfeng.smartmeeting.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itqianfeng.smartmeeting.entity.User;
import com.itqianfeng.smartmeeting.service.UserService;
import com.itqianfeng.smartmeeting.util.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @GetMapping("/list")
    public Map list(int page,int limit){
     //   Map map = new HashMap<>();
        try {
           // List<User> list = userService.list();
            QueryWrapper wrapper=new QueryWrapper<>();
            Page<User> page1 = new Page<>(page,limit);
            IPage<User> obj = userService.page(page1,wrapper);
            return CommonResult.ok("查询成功",obj);
        }catch (Exception e){
            return CommonResult.error("发生异常",-1);
        }
    }
    @GetMapping("/{id}")
    public Map getById(@PathVariable int id){
        try {
            User id1 = userService.getById(id);
            return CommonResult.ok(id1!=null?"查询成功":"用户不存在",id1);
        }catch (Exception e){
            return CommonResult.error("发生异常",-1);
        }
    }
    @PostMapping("/add")
    public Map add(@RequestBody User user){
        Integer flag = userService.saveUserDetails(user);
        String msg="";
        if (flag==1){
            msg="添加成功";
        }else if(flag==2){
            msg="用户名存在";
        }else{
            msg="添加失败";
        }
        return CommonResult.ok(msg,null);
//        try {
//            //添加功能判断用户名是否唯一
//            QueryWrapper<User> wrapper=new QueryWrapper<>();
//            wrapper.eq("user_name",user.getUserName());
//            User user1 = userService.getOne(wrapper);
//            if (user1!=null){
//                return CommonResult.error("用户名已存在",2);
//            }
//            boolean save = userService.save(user);
//            return CommonResult.ok(save?"添加成功":"添加失败",null);
//        }catch (Exception e){
//            return CommonResult.error("发生异常",-1);
//        }
    }
    @PutMapping ("/update/{id}")
    public Map update(@PathVariable(name = "id") Integer id,@RequestBody User user){
        try {
            user.setUserId(id);
            boolean update = userService.updateById(user);
            return CommonResult.ok(update?"修改成功":"修改失败",null);
        }catch (Exception e){
            return CommonResult.error("发生异常",-1);
        }
    }
    @DeleteMapping("/delete/{id}")
    public Map delete(@PathVariable(name = "id") Integer id){
        try {
            boolean update = userService.removeById(id);
            return CommonResult.ok(update?"删除成功":"删除失败",null);
        }catch (Exception e){
            return CommonResult.error("发生异常",-1);
        }
    }
    @DeleteMapping("/delete")
    public Map delete(@RequestBody String[] ids){
        try {
            boolean update = userService.removeByIds(Arrays.asList(ids));
            return CommonResult.ok(update?"删除成功":"删除失败",null);
        }catch (Exception e){
            return CommonResult.error("发生异常",-1);
        }
    }
    @PostMapping("/login")
    public Map login(@RequestBody Map<String,Object> credentials){
    try{
        String userName = (String) credentials.get("userName");
        String passWord = (String) credentials.get("passWord");
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name",userName);
        wrapper.eq("pass_word",passWord);
        User user = userService.getOne(wrapper);
        return CommonResult.ok(user!=null?"登录成功":"用户名或密码错误",user);
    }catch (Exception e){
        return CommonResult.error("发生异常",-1);
    }
    }
}
